/**
* websocket 通讯相关
*/
let GWebsocket = new class {

    private _ws: WebSocket = null;

    /**初始化连接websocket */
    init() {
        this._ws = new WebSocket("ws://localhost:8080");
        this._ws.binaryType = "arraybuffer";
        //监听连接
        this._ws.onopen = () => {
            console.warn("WebSocket 连接成功！");
        };
        //监听接收到信息
        this._ws.onmessage = (msg) => {
            console.warn("接收到信息", msg);
            let info = pb.Player.decode(new Uint8Array(msg.data));
            console.warn("接收到信息", info);
        }

    }

    /**向服务端发送信息 */
    send(info: pb.IPlayer){
        let buffer = pb.Player.encode(info).finish();
        console.warn("发送信息", info, buffer);
        this._ws.send(buffer);
    }
    
}();
window["GWebsocket"] = GWebsocket;